package pl.edu.pw.elka.tinyrepo.client.view;

import java.awt.BorderLayout;
import java.util.concurrent.BlockingQueue;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.tree.TreeModel;

import pl.edu.pw.elka.tinyrepo.client.common.MockRequest;
import pl.edu.pw.elka.tinyrepo.client.common.RepoTreeModel;
import pl.edu.pw.elka.tinyrepo.common.FileProperties;

/**
 * Fasada widoku dla klienta 
 * 
 * @author Anna Stępień
 * @version 12-12-2011
 *
 */
public class View
{
	/** Kolejka blokująca Requestów */
	private final BlockingQueue<MockRequest> requestQueue;
	/** Głowne okno aplikacji */
	private final ClientFrame frame;
	/** Widok na strukturę repozytorium */
	private final RepoTreePanel repoPanel;
	/** Widok na właściwości pliku/kolekcji */
	private final ResourcePropertiesPanel propertiesPanel;
	/** Panel z przyciskami */
	private final ControlPanel controlPanel;
	
	public View(final BlockingQueue<MockRequest> requestQueue)
	{
		this.requestQueue = requestQueue;
		frame = new ClientFrame(requestQueue);
		repoPanel = new RepoTreePanel(requestQueue);
		propertiesPanel = new ResourcePropertiesPanel();
		controlPanel = new ControlPanel(requestQueue);
		
		//ustawienie wszystkich okienek
		
		frame.setLayout(new BorderLayout());
		frame.add(repoPanel, BorderLayout.CENTER);
		frame.add(controlPanel, BorderLayout.NORTH);
		frame.add(propertiesPanel, BorderLayout.LINE_END);
		frame.pack();
	}
	
	public void display()
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			
			@Override
			public void run()
			{
				frame.setVisible(true);
			}
		});
	}
	
	public void displayMessage(final String message)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			
			@Override
			public void run()
			{
				JOptionPane.showMessageDialog(null,message);
			}
		});
	}

	public void displayTree(final TreeModel model)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			
			@Override
			public void run()
			{
				repoPanel.setModel(model);
			}
		});		
	}
	
	public void displayFileProperties(final FileProperties properties)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			
			@Override
			public void run()
			{
				propertiesPanel.displayProperties(properties);
			}
		});			
	}

}
